//
//  NativeModel.m
//  JAGatherAdDemo
//
//  Created by Yc on 2024/2/26.
//

#import "NativeModel.h"
#import "NativeSelfRenderAdView.h"

@implementation NativeModel

- (void)setAdModel:(JAGNativeFeedAdModel *)adModel {
    _adModel = adModel;
    
    if (adModel.isNativeExpress) {
        // 模版广告需进行render 渲染后，模版视图feedView frame才能获取真实高度
        [adModel render];
        _adView = adModel.feedView;
    } else {
        // NativeSelfRenderAdView 自渲染须设置Frame进行初始化，未设置影响广告容器绑定和曝光
        NativeSelfRenderAdView *adView = [[NativeSelfRenderAdView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120)];
        adView.adMeta = adModel.adMaterialMeta;
        __weak typeof(self) ws = self;
        [adView setTapDislikeBlock:^{
            __strong typeof(ws) self = ws;
            if (self.handleCloseAction) {
                self.handleCloseAction(self->_adModel);
            }
        }];
        _adView = adView;
    }
    
}

- (CGFloat)cellHeight {
    if (_adView) {
        return _adView.frame.size.height;
    } else {
        return 44;
    }
    
}

/// 绑定广告数据
- (void)registerDataObject {
    if (self.adModel) {
        if (self.adModel.isNativeExpress) {
            [self.adModel registerContainer];
        } else {
            [(NativeSelfRenderAdView *)self.adView registerDataObject];
        }
    }
}

/// 解绑广告数据
- (void)unregisterDataObject {
    if (self.adModel) {
        if (self.adModel.isNativeExpress) {
            [self.adModel unregisterView];
        } else {
            [(NativeSelfRenderAdView *)self.adView unregisterDataObject];
        }
    }
}

@end
